package com.example.graduatedesigned.adapter;

import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.graduatedesigned.R;
import com.example.graduatedesigned.bean.DeviceHistoryItem;
import com.example.graduatedesigned.utils.DisplayUtils;

/**
 * 两个泛型：数据类型 和 ViewHolder(一般直接写BaseViewHolder)
 */
public class HistoryAdapter extends BaseQuickAdapter<DeviceHistoryItem, BaseViewHolder> {
    private String hotUnit;
    private String wetUnit;
    private int colorHot;
    private int colorWet;
    private String type;

    public HistoryAdapter(int layoutResId, String type) {
        super(layoutResId);
        this.type = type;
    }

    // 提前获取资源 getContext() Please get it after onAttachedToRecyclerView()
    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        hotUnit = getContext().getString(R.string.hot_unit);
        wetUnit = getContext().getString(R.string.wet_unit);
        colorHot = getContext().getColor(R.color.hot);
        colorWet = getContext().getColor(R.color.wet);
    }

    @Override
    protected void convert(@NonNull  BaseViewHolder helper, DeviceHistoryItem item) {
        TextView text = helper.getView(R.id.textValue);
        switch (type) {
            case "temp":
                helper.setText(R.id.textTitle, "温度");
                text.setText(String.format(hotUnit, item.getValue()));
                if (Float.parseFloat(item.getValue()) >= 30f) {
                    text.setTextColor(colorHot);
                }
                break;
            case "humi":
                helper.setText(R.id.textTitle, "湿度");
                text.setText(String.format(wetUnit, item.getValue()));
                if (Float.parseFloat(item.getValue()) >= 50f) {
                    text.setTextColor(colorWet);
                }
                break;
        }
        helper.setText(R.id.dateValue, DisplayUtils.stampToDate(item.getDate()));
    }
}
